package com.szq.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szq.yygh.common.result.Result;
import com.szq.yygh.common.util.MD5;
import com.szq.yygh.hosp.model.hosp.HospitalSet;
import com.szq.yygh.hosp.service.HospitalSetService;
import com.szq.yygh.hosp.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/***
 * All rights Reserved, Designed By www.jx-lab.com
 * @Title: HospitalSetController
 * @Description:
 * @author lixy
 * @date 2021/8/18 11:37
 * @version V1.0
 * @Copyright: www.jx-lab.com Inc. All rights reserved.
 * 注意：本内容仅限于众合景轩信息技术有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {


    @Autowired
    private HospitalSetService hospitalSetService;

    @GetMapping("/findAll")
    @ApiOperation(value = "获取所有医院设置的信息")
    public Result findHospitalSet() {
        return Result.ok(hospitalSetService.list());
    }

    @ApiOperation(value = "根据Id删除医院设置信息")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据医院编码查询医院设置信息")
    @DeleteMapping("/findByHoscode/{hoscode}")
    public Result findByHoscode(@PathVariable String hoscode){
        return Result.ok(hospitalSetService.findByHoscode(hoscode));
    }

    @ApiOperation(value = "分页查询医院设置列表")
    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result pageByParams(@PathVariable int current, @PathVariable int limit,
                               @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        if (!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname", hosname);
        }
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode", hoscode);
        }
        Page<HospitalSet> result = hospitalSetService.page(page, wrapper);
        return Result.ok(result);
    }

    @ApiOperation(value = "保存医院设置信息")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);

        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("根据Id查询医院设置信息")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable String id){
        return Result.ok(hospitalSetService.getById(id));
    }

    @ApiOperation("修改医院设置信息")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("锁定解锁医院")
    @PostMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable String id, @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);

        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation("发送签名密钥")
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable String id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();

        return Result.ok();
    }
}
